package com.boss.thirteen.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.IBaseDateDao;
import com.boss.salary.dao.IPayeeAcctDao;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.entity.PayeeAcct;
import com.boss.salary.entity.SalaryBaseItemize;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import com.boss.thirteen.dao.IThirteenSalaryItemizeDao;
import com.boss.thirteen.service.IThirteenSalaryItemizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ThirteenSalaryItemizeServiceImpl implements IThirteenSalaryItemizeService {

    @Autowired
    private IThirteenSalaryItemizeDao iSalSalaryBaseItemizeDao;
    @Autowired
    private IBaseDateDao baseDateDao;
    @Autowired
    private IAgencyInfoDao agencyInfoDao;
    @Autowired
    private IPayeeAcctDao iPayeeAcctDao;

    @Override
    public PageDTO<SalaryBaseItemize> querySalaryBaseItemizePage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        List<SalaryBaseItemize> salSalarys = iSalSalaryBaseItemizeDao.querySalaryBaseItemizePage(paginationDTO, queryParam);
        int total = iSalSalaryBaseItemizeDao.querySalaryBaseItemizeCount(queryParam);
        PageDTO<SalaryBaseItemize> PageDTO = new PageDTO<>();
        PageDTO.setRows(salSalarys);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public List<SalaryBaseItemize> querySalaryBaseItemize(Map<String, Object> queryParam) {
        return iSalSalaryBaseItemizeDao.querySalaryBaseItemize(queryParam);
    }

    @Override
    public List<Map<String, Object>> queryBaseDataList(Map<String, Object> queryParam) {
        return iSalSalaryBaseItemizeDao.queryBaseDataList(queryParam);
    }

    @Override
    public List<Map<String, Object>> queryPayeeAcctList(UserDTO userDTO ) {
        return iSalSalaryBaseItemizeDao.queryPayeeAcctList(userDTO);
    }

    @Override
    public List<Map<String, Object>> querySalaryBasedataList(String agencyId,UserDTO userDTO) {
        return iSalSalaryBaseItemizeDao.querySalaryBasedataList(agencyId,userDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSalaryBaseItemize(List<SalaryBaseItemize> salaryBaseItemizes,UserDTO userDTO) {
        iSalSalaryBaseItemizeDao.deleteSalaryBaseItemize(salaryBaseItemizes.get(0).getAgencyId());

        List<Map<String, Object>> salaryList=iSalSalaryBaseItemizeDao.querySalaryBasedataList("",userDTO);
        List<AgencyInfo> agencyList=agencyInfoDao.getAllAgency(userDTO);
        // 查询数据库数据看和传过来的数据是否有重复
        List<PayeeAcct> payeeAcctList = iPayeeAcctDao.getAccountInfoList(userDTO.getProvince());
        Map<String,String> agencyCodeMap=agencyList.stream().collect(Collectors.toMap(AgencyInfo::getId,AgencyInfo::getAgencyCode));
        Map<String,String> agencyNameMap=agencyList.stream().collect(Collectors.toMap(AgencyInfo::getId,AgencyInfo::getAgencyName));
        Map<String,String> payeeNameMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctName));
        Map<String,String> payeeBankNameMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctBankName));
        Map<String,String> payeeAcctNoMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctNo));
        Map<String, Object> queryParam=new HashMap();
        queryParam.put("type","salary_type");
        queryParam.put("mof_div_code",userDTO.getProvince());
        List<Map<String, Object>> meritPayCodes=baseDateDao.queryBaseDataList(queryParam);
        Map<String,String> meritPayCodeMap=new HashMap<>();
        for(Map map:meritPayCodes){
            meritPayCodeMap.put(map.get("code").toString(),map.get("name").toString());
        }
        Map<String,String> salaryNameMap=new HashMap<>();
        Map<String,String> salaryCodeMap=new HashMap<>();
        for(Map<String, Object> map:salaryList){
            salaryNameMap.put(map.get("id").toString(),map.get("salaryName").toString());
            salaryCodeMap.put(map.get("id").toString(),map.get("salary_code").toString());
        }
        for (SalaryBaseItemize salaryBaseItemize : salaryBaseItemizes) {
            if(null==payeeBankNameMap.get(salaryBaseItemize.getPayeeAcctId())){
                throw new AppRuntimeException("存在已删除收款户信息，请修改后重新保存");
            }
            salaryBaseItemize.setId(SysUtils.generateGuid());
            salaryBaseItemize.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryBaseItemize.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            salaryBaseItemize.setIsDeleted(SystemDictConstant.IS_FALSE);
            salaryBaseItemize.setSalaryBaseCode(salaryCodeMap.get(salaryBaseItemize.getSalaryBaseId()));
            salaryBaseItemize.setSalaryBaseName(salaryNameMap.get(salaryBaseItemize.getSalaryBaseId()));
            salaryBaseItemize.setAgencyCode(agencyCodeMap.get(salaryBaseItemize.getAgencyId()));
            salaryBaseItemize.setAgencyName(agencyNameMap.get(salaryBaseItemize.getAgencyId()));
            salaryBaseItemize.setPersonStateName(meritPayCodeMap.get(salaryBaseItemize.getPersonState()));
            salaryBaseItemize.setPayeeAcctBankName(payeeBankNameMap.get(salaryBaseItemize.getPayeeAcctId()));
            salaryBaseItemize.setPayeeAcctName(payeeNameMap.get(salaryBaseItemize.getPayeeAcctId()));
            salaryBaseItemize.setPayeeAcctNo(payeeAcctNoMap.get(salaryBaseItemize.getPayeeAcctId()));
            if(SysUtils.isNullOrEmpty(salaryBaseItemize.getRemark())){
                salaryBaseItemize.setRemark("");
            }
            iSalSalaryBaseItemizeDao.insertSalaryBaseItemize(salaryBaseItemize,userDTO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSalaryBaseItemizeBatch(SalaryBaseItemize salaryBaseItemize,UserDTO userDTO) {
        //查询当前配置的工资类型工资项是否存在一配置信息，存在返回单位id后续判断使用
       List<String> checkAgencyIds = iSalSalaryBaseItemizeDao.salaryBaseItemizeCheck(salaryBaseItemize);
        List<Map<String, Object>> salaryList=iSalSalaryBaseItemizeDao.querySalaryBasedataList(null,userDTO);
        List<AgencyInfo> agencyList=agencyInfoDao.getAllAgency(userDTO);
        // 查询数据库数据看和传过来的数据是否有重复
        List<PayeeAcct> payeeAcctList = iPayeeAcctDao.getAccountInfoList(userDTO.getProvince());
        Map<String,String> agencyCodeMap=agencyList.stream().collect(Collectors.toMap(AgencyInfo::getId,AgencyInfo::getAgencyCode));
        Map<String,String> agencyNameMap=agencyList.stream().collect(Collectors.toMap(AgencyInfo::getId,AgencyInfo::getAgencyName));
        Map<String,String> payeeNameMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctName));
        Map<String,String> payeeBankNameMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctBankName));
        Map<String,String> payeeAcctNoMap=payeeAcctList.stream().collect(Collectors.toMap(PayeeAcct::getId,PayeeAcct::getPayeeAcctNo));
        Map<String, Object> queryParam=new HashMap();
        queryParam.put("type","salary_type");
        queryParam.put("mof_div_code",userDTO.getProvince());
        List<Map<String, Object>> meritPayCodes=baseDateDao.queryBaseDataList(queryParam);
        Map<String,String> meritPayCodeMap=new HashMap<>();
        for(Map map:meritPayCodes){
            meritPayCodeMap.put(map.get("code").toString(),map.get("name").toString());
        }
        Map<String,String> salaryNameMap=new HashMap<>();
        Map<String,String> salaryCodeMap=new HashMap<>();
        for(Map<String, Object> map:salaryList){
            salaryNameMap.put(map.get("id").toString(),map.get("salaryName").toString());
            salaryCodeMap.put(map.get("id").toString(),map.get("salary_code").toString());
        }
        List<String> agencyIds = salaryBaseItemize.getAgencyIds();
        for (String agencyId : agencyIds) {
            if (checkAgencyIds.contains(agencyId)) {//若存在已配置信息则不进行保存操作，继续下一条
                continue;
            }
            SalaryBaseItemize sbz = new SalaryBaseItemize();
            sbz.setAgencyId(agencyId);
            sbz.setSalaryBaseId(salaryBaseItemize.getSalaryBaseId());
            sbz.setSalaryBaseCode(salaryBaseItemize.getSalaryBaseCode());
            sbz.setMofDivCode(salaryBaseItemize.getMofDivCode());
            sbz.setPersonState(salaryBaseItemize.getPersonState());
            sbz.setPayeeAcctId(salaryBaseItemize.getPayeeAcctId());
            sbz.setPayeeAcctBankName(payeeBankNameMap.get(salaryBaseItemize.getPayeeAcctId()));
            sbz.setPayeeAcctName(payeeNameMap.get(salaryBaseItemize.getPayeeAcctId()));
            sbz.setPayeeAcctNo(payeeAcctNoMap.get(salaryBaseItemize.getPayeeAcctId()));
            sbz.setId(SysUtils.generateGuid());
            sbz.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            sbz.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            sbz.setSalaryBaseCode(salaryCodeMap.get(salaryBaseItemize.getSalaryBaseId()));
            sbz.setSalaryBaseName(salaryNameMap.get(salaryBaseItemize.getSalaryBaseId()));
            sbz.setIsDeleted(SystemDictConstant.IS_FALSE);
            sbz.setRemark(salaryBaseItemize.getRemark());
            sbz.setAgencyCode(agencyCodeMap.get(agencyId));
            sbz.setAgencyName(agencyNameMap.get(agencyId));
            sbz.setPersonStateName(meritPayCodeMap.get(salaryBaseItemize.getPersonState()));
            iSalSalaryBaseItemizeDao.insertSalaryBaseItemize(sbz,userDTO);
        }
    }

    @Override
    public void deleteSalaryBaseItemize(String id) {
        iSalSalaryBaseItemizeDao.deleteSalaryBaseItemize(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalaryBaseItemize(SalaryBaseItemize salaryBaseItemize) {
        salaryBaseItemize.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iSalSalaryBaseItemizeDao.updateSalaryBaseItemize(salaryBaseItemize);
    }

    @Override
    public    List<AgencyInfo>  queryAgencyInfo(List<String> childrenId,String salaCardBank,String personState) {
        return iSalSalaryBaseItemizeDao.queryAgencyInfo(childrenId,salaCardBank,personState);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalaryBaseItemizeAll(UserDTO userDTO) {
        iSalSalaryBaseItemizeDao.deleteSalaryBaseItemizeAll(userDTO.getProvince());
    }
}
